using RealtimeCollabGame.Application.Commands;
using RealtimeCollabGame.Application.Interfaces;

namespace RealtimeCollabGame.Application.Handlers;

public class SetPlayerReadyCommandHandler
{
    private readonly IGameRepository _gameRepository;

    public SetPlayerReadyCommandHandler(IGameRepository gameRepository)
    {
        _gameRepository = gameRepository;
    }

    public async Task HandleAsync(SetPlayerReadyCommand command)
    {
        var game = await _gameRepository.GetByIdAsync(command.GameId);
        if (game == null)
            throw new InvalidOperationException("游戏不存在");

        game.SetPlayerReady(command.PlayerId, command.IsReady);
        await _gameRepository.UpdateAsync(game);
    }
}